<?php
// +----------------------------------------------------------------------
// | Desc: [ Better-钱包记录 ]
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\index\model;
use think\facade\Db;
use app\common\model\Common;

class UserCashLog extends Common
{
    //---------------------------------------------前端---------------------------------------------------------------
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableDataIndex($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        $where[] = ['u.id', '=', $post['user_id']];
        if(isset($post['desc']) && !empty($post['desc'])){
            $where[] = ['ucl.desc', 'like', '%'.$post['desc'].'%'];
        }
        if(isset($post['stime']) && !empty($post['stime'])){
            $start_time = strtotime($post['stime'] . " 00:00:00") - 13*60*60;
            $end_time   = strtotime((empty($post['ltime'])?"2023-12-31":$post['ltime'])  . " 23:59:59") - 13*60*60;
            $where[] = ['ucl.ctime', 'between', "{$start_time},{$end_time}"];
        }

        $list = Db::name('user_cash_log')->alias('ucl')
            ->leftJoin('user u','u.id=ucl.user_id')
            ->field('ucl.id,username,ucl.ctime,ucl.desc,ucl.amount,ucl.balance,ucl.type')
            ->order('ucl.id desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableData($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        if(isset($post['desc']) && !empty($post['desc'])){
            $where[] = ['ucl.desc', 'like', '%'.$post['desc'].'%'];
        }
        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode('_' , $post['datetime']);
            $start_time = strtotime($time_list[0] . " 00:00:00") - 8*60*60;
            $end_time   = strtotime($time_list[1] . " 23:59:59") - 8*60*60;
            $where[] = ['ucl.ctime', 'between', "{$start_time},{$end_time}"];
        }

        $list = Db::name('user_cash_log')->alias('ucl')
            ->field('ucl.ctime,ucl.desc,ucl.amount,ucl.balance,ucl.type')
            ->order('ucl.ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    /**
     * 会员到首页显示充值列表(只显示成功的)
     * @param $post
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function indexHomeTable($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        $where[] = ['user_id', '=', $post['user_id']];
        $list = Db::name('user_cash_log')
            ->where($where)
            ->order('ctime desc')
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();
        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }
}